{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7332a2d0-10c9-40f2-8bda-9a2ec630f13f",
   "metadata": {},
   "source": [
    "Chapter 21\n",
    "# 无向图度矩阵\n",
    "Book_6《数据有道》 | 鸢尾花书：从加减乘除到机器学习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ac43e145-b958-4c37-be88-32516fe9bdf0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import networkx as nx\n",
    "import numpy as np\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8aa6b49e-bbb9-48e1-94ca-6034b04f6fe3",
   "metadata": {},
   "outputs": [],
   "source": [
    "G = nx.Graph()\n",
    "# 创建无向图的实例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6d040088-8f36-4e83-9203-4536c4d8188a",
   "metadata": {},
   "outputs": [],
   "source": [
    "G.add_nodes_from(['a', 'b', 'c', 'd'])\n",
    "# 添加多个顶点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f37a2cdf-1e76-4a46-bf35-5492af13e615",
   "metadata": {},
   "outputs": [],
   "source": [
    "G.add_edges_from([('a','b'),('b','c'),\n",
    "                  ('b','d'),('c','d'),\n",
    "                  ('c','a')])\n",
    "# 增加一组边"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "135de57a-48aa-4d48-9131-62deabb7f6d5",
   "metadata": {},
   "source": [
    "## 度矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c2821e08-e748-41bd-9288-d73c02c145b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 1, 0],\n",
       "       [1, 0, 1, 1],\n",
       "       [1, 1, 0, 1],\n",
       "       [0, 1, 1, 0]], dtype=int32)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = nx.adjacency_matrix(G).todense()\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fccbafd1-66d6-48ac-ba4a-16ae7984afd2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 3, 2], dtype=int32)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D = A.sum(axis = 0)\n",
    "D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "27232e9e-84b2-4b5a-93ba-77d3e808c967",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values([2, 3, 3, 2])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict(G.degree()).values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "07a8a484-5d16-4c1c-b2d7-6955bc9dff45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 0, 0, 0],\n",
       "       [0, 3, 0, 0],\n",
       "       [0, 0, 3, 0],\n",
       "       [0, 0, 0, 2]], dtype=int32)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D = np.diag(D)\n",
    "D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "da3cf9e8-237f-4469-997f-b60edaffa918",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAegAAAGiCAYAAAAsk1UGAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8g+/7EAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAqC0lEQVR4nO3de3xU9Z3/8fdMSGYikAiJJOGmaNhwCTe5JcEqWi5FiwZbSll/hmUBbQkKhkWMC1Jx9zFdEPDGVYqwWn5QaoUWUc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UVORK5CYII=\n",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.heatmap(D, cmap = 'Blues', \n",
    "            annot = True, fmt = '.0f',\n",
    "            xticklabels = list(G.nodes), \n",
    "            yticklabels = list(G.nodes),\n",
    "            linecolor = 'k', square = True,\n",
    "            linewidths = 0.2)\n",
    "plt.savefig('度矩阵.svg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e60749b0-77b5-4077-b05f-4fde3a14dc90",
   "metadata": {},
   "source": [
    "## 空手道俱乐部"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3c1427c5-3e14-42e1-8d2d-bb75dbeab0ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "G_karate = nx.karate_club_graph() \n",
    "# 空手道俱乐部图\n",
    "pos_karate = nx.spring_layout(G_karate,seed=2) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "46c49faf-eeab-49b8-b3f8-f12d705e9ae8",
   "metadata": {},
   "outputs": [],
   "source": [
    "dict_degrees = dict(G_karate.degree())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "28444e39-90d7-4118-bc3d-a597578c623e",
   "metadata": {},
   "outputs": [],
   "source": [
    "D_karate = np.diag(list(dict_degrees.values()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6190a404-2709-4a01-bd91-c1ba61fd578b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[16,  0,  0, ...,  0,  0,  0],\n",
       "       [ 0,  9,  0, ...,  0,  0,  0],\n",
       "       [ 0,  0, 10, ...,  0,  0,  0],\n",
       "       ...,\n",
       "       [ 0,  0,  0, ...,  6,  0,  0],\n",
       "       [ 0,  0,  0, ...,  0, 12,  0],\n",
       "       [ 0,  0,  0, ...,  0,  0, 17]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D_karate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d04915b5-fd13-4d1f-b2d6-0d24c54e8eba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYUAAAGFCAYAAAASI+9IAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8g+/7EAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAJK0lEQVR4nO3dsWveRRwG8DdioYEIHWKndAgdG0Uo0tVF6OQuTi4u/g066N/g4uJU3J0KLq5FCqLpWDI0U81QMJBChzj1IVPuvZBf7967z2d++H3fHtKHK9zXrfPz8/MVAKxWq/da/wAA+qEUAAilAEAoBQBCKQAQSgGAUAoAhFIAIN5fN7i1tbXk7wBgYeu8VV67FFar1ermZz8UM6//+H61Wq1WZ2/Kw7dvbF17dolvyvYxf+Rs6/kjZ1vP7y1b4p+PAAilAEAoBQBCKQAQSgGAUAoAhFIAIJQCALG17v+O04tmgM22zl/3bgoARNWai5qn1F//+ncx+8uXH1d/15N26wJGzLaeP3K29fzesiVuCgCEUgAglAIAoRQACKUAQCgFAEIpABBKAYCw5gJgEtZcAFBlsTUXNdkvfv6zmP3tm0/X+m5Pz8lHy7aeP3K29fyRs63n95YtcVMAIJQCAKEUAAilAEAoBQBCKQAQSgGAUAoAhDUXAJOw5gKAKl2suajJPnr64tLcV/fvLDp/5mzr+SNnW88fOdt6fm/ZEjcFAEIpABBKAYBQCgCEUgAglAIAoRQACKUAQFhzATAJay4AqLJxay7WfdJ+eHxa/ObB3s61zx8523r+yNnW80fOtp7fW7bETQGAUAoAhFIAIJQCAKEUAAilAEAoBQBCKQAQ1lwATMKaCwCqDLvmouabT56/KmYf3L1V/d3Rsq3nj5xtPX/kbOv5vWVL3BQACKUAQCgFAEIpABBKAYBQCgCEUgAglAIAYc0FwCSsuQC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hzQXAJKy5AKCKNReyzraDbOv5I2dbz38X2ZuffFvMvv7rp2JmtXJTAOACpQBAKAUAQikAEEoBgFAKAIRSACCUAgBhzQXAJNb5637tF81rdgcAG8w/HwEQSgGAUAoAhFIAIJQCAKEUAAilAEAoBQBCKQAQ/wPD/YdzoZQ8BQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.heatmap(D_karate, cmap = 'Blues', \n",
    "            annot = False, \n",
    "            xticklabels = [], \n",
    "            yticklabels = [],\n",
    "            cbar = False,\n",
    "            linecolor = 'k', square = True,\n",
    "            linewidths = 0.1)\n",
    "plt.savefig('度矩阵，空手道俱乐部.svg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd248be9-cb8e-4458-81d9-4431d7ead46d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
